<?php

class Admin_corner_description_Controller extends Template_Controller {

	

	public $search;

	public $template = 'admin/index';

	

    public function __construct()

    {

        parent::__construct();

        

        $this->search = array('keyword' => '','page' => 0);

        

        $this->_get_submit();

    }

    

	public function __call($method, $arguments)

	{

		// Disable auto-rendering

		$this->auto_render = FALSE;

	}

	

	function _get_submit()

	{

		if($this->session->get('error_msg'))

			$this->template->error_msg = $this->session->get('error_msg');

		if($this->session->get('warning_msg'))

			$this->template->warning_msg = $this->session->get('warning_msg');

		if($this->session->get('success_msg'))

			$this->template->success_msg = $this->session->get('success_msg');

		if($this->session->get('info_msg'))

			$this->template->info_msg = $this->session->get('info_msg');

		if($this->session->get('frm'))			

			$this->template->content->mr = $this->session->get('frm');

	}

	

    public function index()

    {	
		$id=1;
		$this->template->content = new View('admin_corner/frm_description');

    	$this->_list_language();

    	$list = $this->_show_corner($id);

    	$mr = $list[0]['languages'];
		$this->template->content->mr = $mr;        

    }

    

    private function _list_language()

    {

		$model_lang = new Language_Model();		

		$this->template->content->list_language = $model_lang->get_with_active();

	}

	

	public function _show_corner($id='1')

	{

		if($id)

			$this->db->where('corner_description.corner_id',$id);

		$model = new Corner_description_Model();    	

    	$mlist = $model->get();

    	for($i=0; $i<count($mlist); $i++)

		{

			$model_lang = new Language_Model();			

			$list_lang = $model_lang->get_with_active();

			for($j=0; $j<count($list_lang); $j++)

			{

				$model_content = new Corner_description_Model();

				$this->db->where('corner_id',$mlist[$i]['corner_id']);

				$this->db->where('languages_id',$list_lang[$j]['languages_id']);

				$list = $model_content->get();

				//$list = $model_content->get($mlist[$i]['corner_id'],$list_lang[$j]['languages_id']);

				if($list){

				$list = array_merge($mlist[$i],$list[0]);				

				}	

				$list_lang[$j] = $list;	

			}

			$mlist[$i]['languages'] = $list_lang;

		}

		return $mlist;

	}

	

    public function edit()

    {	

    	$id=1;
		$this->template->content = new View('admin_corner/frm_description');

    	$this->_list_language();

    	$list = $this->_show_corner($id);

    	$mr = $list[0]['languages'];

		$this->template->content->mr = $mr;

    }
	
	

    private function _get_record()

    {

    	$form = array

	    (

	    	'hd_id'	=> '',

	    	'txt_description' => '',

	       

	    );

		

		$model_lang = new Language_Model();

		$list = $model_lang->get_with_active();	

		for($i=0; $i<count($list); $i++)

		{

			$form['txt_description'.$list[$i]['languages_id']] = '';

			

		}

	 

		$errors = $form;

		

		if($_POST)

    	{

    		$post = new Validation($_POST);

    		

			$post->pre_filter('trim', TRUE);

			for($i=0; $i<count($list); $i++){

				$post->add_rules('txt_description'.$list[$i]['languages_id'],'required');

				

			}

				

			if($post->validate())

 			{

 				$form = arr::overwrite($form, $post->as_array());

 				return $form; 				

			} else {

				$errors = arr::overwrite($errors, $post->errors('corner_validation'));

				$str_error = '';

	            for($i=0; $i<count($list); $i++)

				{

					if ($errors['txt_description'.$list[$i]['languages_id']])

						$str_error .= $errors['txt_description'.$list[$i]['languages_id']].'<br>';

				}

				
				$this->session->set_flash('error_msg',$str_error);

				

				$hd_id = $this->input->post('hd_id');

				if($form['hd_id'])

					url::redirect('admin_description/edit/'.$form['hd_id']);

				else

					url::redirect('admin_corner');

				die();

			}

        }

    }

   
	private function _save_corner_content($record)

	{

		$model_lang = new Language_Model();

		$list = $model_lang->get_with_active();
$hd_id = $this->input->post('hd_id');
			for($i=0; $i<count($list); $i++)

		{

			
				$this->db->update('corner_description', 

					array('corner_description' => $record['txt_description'.$list[$i]['languages_id']]

						  ), 

					array('corner_id' => $hd_id,

						  'languages_id' => $list[$i]['languages_id']

						 )

					);

			
		}

	}

	

    public function save()

    {

    	$hd_id = $this->input->post('hd_id');

    	

    	$record = $this->_get_record();

		

		if($record)

		{

			
			$this->_save_corner_content($record);

			

			if(!$this->input->post('hd_id'))

				$this->session->set_flash('success_msg',Kohana::lang('errormsg_lang.msg_data_add'));

			else

				$this->session->set_flash('success_msg',Kohana::lang('errormsg_lang.msg_data_save'));

		}

		url::redirect('admin_corner_description');

		die();    	

    }

    

   

}

?>